package com.atguigu.auth.controller;


import com.atguigu.auth.service.SysMenuService;
import com.atguigu.common.result.Result;
import com.atguigu.model.system.SysMenu;
import com.atguigu.vo.system.AssginMenuVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2024-01-19
 */
@Api(tags = "菜单管理接口")
@RestController
@RequestMapping("admin/system/sysMenu")
public class SysMenuController {
    private final SysMenuService sysMenuService;


    public SysMenuController(SysMenuService sysMenuService) {
        this.sysMenuService = sysMenuService;
    }



    /**
     * 所有菜单和角色分配菜单
     * @param roleId 角色id
     */
    @ApiOperation("所有菜单和角色分配菜单")
    @GetMapping("doRoleMenu/{roleId}")
    public Result<Object> doRoleMenu(@PathVariable long roleId) {
        return sysMenuService.doRoleMenu(roleId);
    }

    /**
     * 角色分配菜单
     * @param menuVo menuVo
     * @return Result
     */
    @ApiOperation("角色分配菜单")
    @PostMapping("assignMenus")
    public Result<Object> assignMenus(@RequestBody AssginMenuVo menuVo){
        return sysMenuService.assignMenus(menuVo);
    }





    /**
     * 菜单树
     *
     * @return Result
     */
    @ApiOperation("菜单树")
    @GetMapping("tree")
    public Result<Object> tree() {
        List<SysMenu> menuList = sysMenuService.tree();
        return Result.ok(menuList);
    }

    /**
     * 菜单列表
     *
     * @return Result
     */
    @ApiOperation("菜单列表")
    @GetMapping("list")
    public Result<Object> list() {
        return Result.ok(sysMenuService.list());
    }


    /**
     * 菜单新增
     *
     * @param menu SysMenu
     * @return Result
     */
    @ApiOperation("新增菜单")
    @PostMapping("save")
    public Result<Object> save(@RequestBody SysMenu menu) {
        sysMenuService.save(menu);
        return Result.ok();
    }

    /**
     * 菜单修改
     *
     * @param menu SysMenu
     * @return Result
     */
    @ApiOperation("修改菜单")
    @PostMapping("update")
    public Result<Object> update(@RequestBody SysMenu menu) {
        sysMenuService.updateById(menu);
        return Result.ok();
    }

    /**
     * 菜单删除
     *
     * @param id 菜单id
     * @return Result
     */
    @ApiOperation("菜单删除")
    @DeleteMapping("delete/{id}")
    public Result<Object> delete(@PathVariable long id) {
        sysMenuService.removeMenuById(id);
        return Result.ok();
    }
}

